#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;



namespace U32
{
	
		struct data
				{
					long long sum;
					long long int n;
					data()
					{
						sum = 0;
						n = 0;
			
					}		
		
				};


	class Statistic
	{
	private:
		vector <int> SV;
		vector <struct data> Group;
		
	public:

		void readFromFile()
					{
							ifstream fin;
							fin.open("salary.txt");
							int salary;
							while (fin.good())
							{
									fin >> salary;
									if (salary < 1000000)
									{
										SV.push_back(salary);
									}
							}
					}

		
		
		void group_by_sal(long long int interval)
					{
						data obj; 
						
						for (int i = 0; i < SV.size(); i++) 
						{
							if (SV[i] > interval && SV[i] < interval + 1000 )
							{
								obj.sum += SV[i];
								obj.n += 1;
							}

						}
						
						if (obj.sum != 0){	Group.push_back(obj);	}

						if (interval < 1000000){group_by_sal(interval + 1000);	}
					}
		
		int calculate_mean()
                {
                        int NumberOfSal = 0;
                        long long  sum = 0;
                        for (int i = 0; i < Group.size(); i++)
                        {
                                NumberOfSal += Group[i].n;
                                if (Group[i].sum > 0)
                                {
                                        sum += Group[i].sum;
                                }
                        }

                        return sum / NumberOfSal;
                }

		void clear_vector()
		{			
			
		}

		int percentile(float procent)
		{
			int NumberOfSal = 0;
            long long  sum = 0;
			long long int sal = 0;
			int p = 0;

			for (int i = 0; i < Group.size(); i++)
			{
				NumberOfSal += Group[i].n;
            }
			
			for (int i = 0; i < Group.size(); i++)
			{
				p += Group[i].n;
				sum += Group[i].sum;


				if (p > NumberOfSal * procent)
				{
					return i * 1000;
				}
            }

			return 0;
		}

		Statistic(void)
		{
		}

		~Statistic(void)
		{
		}
	};
}
